package com.dlc.yunguang.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.yunguang.model.HandheldDevice;
import com.dlc.yunguang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by John on 2017/7/18.
 */

public class HandheldDeviceAdapter extends BaseRecyclerAdapter<HandheldDevice> {

    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_handheld_device;
    }

    @Override
    public void onBindViewHolder(CommonHolder holder, int position) {

        HandheldDevice item = getItem(position);

        holder.setText(R.id.tv_name, item.getName());
        ImageView ivOk = holder.getImage(R.id.iv_ok);
        TextView tvStatus = holder.getText(R.id.tv_connect_status);

        if (item.isConnected()) {
            ivOk.setVisibility(View.VISIBLE);
            tvStatus.setText(R.string.connected);
            tvStatus.setEnabled(true);
        } else {
            ivOk.setVisibility(View.GONE);
            tvStatus.setText(R.string.unconnected);
            tvStatus.setEnabled(false);
        }
    }



    /**
     * 清除未连接的设备
     */
    public void cleanNoConnectDev(){
        List<HandheldDevice> connect = new ArrayList<>();
        for(HandheldDevice device:getData()){
            if(device.isConnected())
                connect.add(device);
        }

        setNewData(connect);

    }

    /**
     * 蓝牙连接断开后，清除已连接设备
     */
    public void setNoConnectDev(){
        List<HandheldDevice> connect = new ArrayList<>();
        for(HandheldDevice device:getData()){
            if(device.isConnected())
                device.setConnected(false);
        }
        setNewData(connect);
    }


}
